package com.onlinetp.controller;

import com.onlinetp.commons.Result;
import com.onlinetp.util.RSAUtil;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.security.Key;
import java.util.Map;

@RestController
@RequestMapping("t")
public class TestController {

    @RequestMapping("test")
    public String test(){
        return "test";
    }

    @RequestMapping("getKey")
    public Result getKey(HttpSession session){
        Map<String, Key> rsaKey = (Map<String, Key>) session.getAttribute("keys");
        if(rsaKey == null){
            rsaKey = RSAUtil.getRSAKey();
            session.setAttribute("keys",rsaKey);
        }

        byte[] encode = Base64Utils.encode(rsaKey.get(RSAUtil.PB_KEY).getEncoded());
        return Result.success(new String(encode));

    }

    @RequestMapping("sendKey")
    public Result sendKey(Map<String,String> map,HttpSession session){
        System.out.println(map.get("clientKey"));
        session.setAttribute(RSAUtil.CLI_PB_KEY,map.get("clientKey"));
        return Result.success();

    }

}
